package com.myapp.ruyixing.http;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import com.myapp.ruyixing.model.ConsumptionRecordList;
import com.myapp.ruyixing.model.ConsumptionRecordList.ConsumptionRecord;

public class ConsumptionRecordListParser extends RYJSONParser {
	@Override
	public Object parse(JSONObject jsonObject) {
		if (getErrno(jsonObject) != NO_ERROR) {
			return null;
		}

		ConsumptionRecordList consumptionRecordList = new ConsumptionRecordList();
		consumptionRecordList.list = new ArrayList<ConsumptionRecordList.ConsumptionRecord>();

		JSONObject data = jsonObject.optJSONObject("data");
		consumptionRecordList.pn = data.optInt("pn");
		consumptionRecordList.rn = data.optInt("pn");
		consumptionRecordList.total = data.optInt("total");

		JSONArray list = data.optJSONArray("list");

		for (int i = 0; list != null && i < list.length(); i++) {
			JSONObject record = list.optJSONObject(i);
			if (record != null) {
				ConsumptionRecord consumptionRecord = new ConsumptionRecord();
				consumptionRecord.content = record.optString("content", "");
				consumptionRecord.datetime = record.optLong("datetime", 0);
				consumptionRecord.mobile = record.optString("mobile", "");
				consumptionRecord.name = record.optString("name", "");
				consumptionRecord.shopname = record.optString("shopname", "");
				consumptionRecord.status = record.optString("status", "");
				consumptionRecordList.list.add(consumptionRecord);
			}
		}

		return consumptionRecordList;

	}
}
